﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using XinYi.CrookedShowMode;

namespace XinYi.CrookedShow.BLL
{
    public class BrandService : BaseService<Brand>
    {
        public List<BrandInfo> GetRoleList()
        {
            //创建List对象
            List<BrandInfo> adminUserQueries = new List<BrandInfo>();
            //创建实例
            using (var db = new CrookedShowEntities())
            {
                //select b.Id,b.Logo,b.Name,COUNT(BrandId) as Counts from dbo.Brand as b left join dbo.Product_Spu as p on b.Id 
                //    = p.BrandId group by b.Id,b.Logo,b.Name
                var result = from a in db.Brand
                             join b in db.Product_Spu
                             on a.Id equals b.BrandId into aa

                             from ab in aa.DefaultIfEmpty()
                             group new { a,ab} by new { a.Id,a.Name,a.Logo,a.Status,a.UpdateTime} into e
                             select new BrandInfo()
                             {
                                 Id=e.Key.Id,
                                 Name=e.Key.Name,
                                 Logo=e.Key.Logo,
                                 Status=e.Key.Status,
                                 UpdateTime=e.Key.UpdateTime.Value,
                                 Counts = e.FirstOrDefault().ab.One_Category_Id == null ? 0 : e.Count()
                             };
               return result.ToList();
            }
        }

        #region 删除
        public bool DelBrand(Brand entity)
        {
            using (var db = new CrookedShowEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        #region 事务提交
                        //先删商品表
                        BaseService<Product_Spu> baseService = new BaseService<Product_Spu>();
                        //根据匹配表I的匹配商品表的外键品牌id，比较删除
                        Expression<Func<Product_Spu, bool>> Product_SpuWherelambda = c => c.BrandId == entity.Id;
                        List<Product_Spu> product_Spu = baseService.QueryList(Product_SpuWherelambda);
                        baseService.BatchDelete(product_Spu);                      
                        BaseService<Brand> baseService2 = new BaseService<Brand>();
                        baseService2.Delete(entity);
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                        #endregion

                        
                    }
                     catch (Exception ex)
                    {
                        tran.Rollback();
                        Console.WriteLine(ex.Message);
                        return false;
                    }
                }
            }
            return true;
        }
        #endregion
    }
}
